home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / test / shape_test.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  4KB  |  194 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <GL/glut.h>
  9.  
  10. float w, h;
  11.  
  12. GLfloat light_diffuse[] =
  13. {1.0, 1.0, 1.0, 1.0};
  14. GLfloat light_position[] =
  15. {1.0, 1.0, 1.0, 0.0};
  16. GLUquadricObj *qobj;
  17.  
  18. void
  19. reshape(int nw, int nh)
  20. {
  21.   w = nw;
  22.   h = nh;
  23. }
  24.  
  25. void
  26. render(int shape)
  27. {
  28.   switch (shape) {
  29.   case 1:
  30.     glPushMatrix();
  31.     glScalef(1.2, 1.2, 1.2);
  32.     glutWireSphere(1.0, 20, 20);
  33.     glPopMatrix();
  34.     break;
  35.   case 10:
  36.     glPushMatrix();
  37.     glScalef(1.2, 1.2, 1.2);
  38.     glEnable(GL_LIGHTING);
  39.     glutSolidSphere(1.0, 20, 20);
  40.     glDisable(GL_LIGHTING);
  41.     glPopMatrix();
  42.     break;
  43.   case 2:
  44.     glPushMatrix();
  45.     glRotatef(-90, 1.0, 0.0, 0.0);
  46.     glutWireCone(1.0, 1.3, 20, 20);
  47.     glPopMatrix();
  48.     break;
  49.   case 11:
  50.     glPushMatrix();
  51.     glRotatef(-90, 1.0, 0.0, 0.0);
  52.     glEnable(GL_LIGHTING);
  53.     glutSolidCone(1.0, 1.3, 20, 20);
  54.     glDisable(GL_LIGHTING);
  55.     glPopMatrix();
  56.     break;
  57.   case 3:
  58.     glPushMatrix();
  59.     glRotatef(-20, 0.0, 0.0, 1.0);
  60.     glScalef(1.8, 1.8, 1.8);
  61.     glutWireCube(1.0);
  62.     glPopMatrix();
  63.     break;
  64.   case 12:
  65.     glPushMatrix();
  66.     glRotatef(-20, 0.0, 0.0, 1.0);
  67.     glScalef(1.8, 1.8, 1.8);
  68.     glEnable(GL_LIGHTING);
  69.     glutSolidCube(1.0);
  70.     glDisable(GL_LIGHTING);
  71.     glPopMatrix();
  72.     break;
  73.   case 4:
  74.     glPushMatrix();
  75.     glScalef(0.9, 0.9, 0.9);
  76.     glutWireTorus(0.5, 1.0, 15, 15);
  77.     glPopMatrix();
  78.     break;
  79.   case 13:
  80.     glPushMatrix();
  81.     glScalef(0.9, 0.9, 0.9);
  82.     glEnable(GL_LIGHTING);
  83.     glutSolidTorus(0.5, 1.0, 15, 15);
  84.     glDisable(GL_LIGHTING);
  85.     glPopMatrix();
  86.     break;
  87.   case 5:
  88.     glPushMatrix();
  89.     glScalef(0.8, 0.8, 0.8);
  90.     glutWireDodecahedron();
  91.     glPopMatrix();
  92.     break;
  93.   case 14:
  94.     glPushMatrix();
  95.     glScalef(0.8, 0.8, 0.8);
  96.     glEnable(GL_LIGHTING);
  97.     glutSolidDodecahedron();
  98.     glDisable(GL_LIGHTING);
  99.     glPopMatrix();
  100.     break;
  101.   case 6:
  102.     glPushMatrix();
  103.     glScalef(0.9, 0.9, 0.9);
  104.     glutWireTeapot(1.0);
  105.     glPopMatrix();
  106.     break;
  107.   case 15:
  108.     glPushMatrix();
  109.     glScalef(0.9, 0.9, 0.9);
  110.     glEnable(GL_LIGHTING);
  111.     glutSolidTeapot(1.0);
  112.     glDisable(GL_LIGHTING);
  113.     glPopMatrix();
  114.     break;
  115.   case 7:
  116.     glutWireOctahedron();
  117.     break;
  118.   case 16:
  119.     glEnable(GL_LIGHTING);
  120.     glutSolidOctahedron();
  121.     glDisable(GL_LIGHTING);
  122.     break;
  123.   case 8:
  124.     glPushMatrix();
  125.     glScalef(1.2, 1.2, 1.2);
  126.     glutWireTetrahedron();
  127.     glPopMatrix();
  128.     break;
  129.   case 17:
  130.     glPushMatrix();
  131.     glScalef(1.2, 1.2, 1.2);
  132.     glEnable(GL_LIGHTING);
  133.     glutSolidTetrahedron();
  134.     glDisable(GL_LIGHTING);
  135.     glPopMatrix();
  136.     break;
  137.   case 9:
  138.     glutWireIcosahedron();
  139.     break;
  140.   case 18:
  141.     glEnable(GL_LIGHTING);
  142.     glutSolidIcosahedron();
  143.     glDisable(GL_LIGHTING);
  144.     break;
  145.   }
  146. }
  147.  
  148. void
  149. display(void)
  150. {
  151.   int i, j;
  152.  
  153.   glViewport(0, 0, w, h);
  154.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  155.   for (j = 0; j < 6; j++) {
  156.     for (i = 0; i < 3; i++) {
  157.       glViewport(w / 3 * i, h / 6 * j, w / 3, h / 6);
  158.       render(18 - (j * 3 + (2 - i)));
  159.     }
  160.   }
  161. }
  162.  
  163. int
  164. main(int argc, char **argv)
  165. {
  166.   glutInit(&argc, argv);
  167.   glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGB);
  168.   glutInitWindowSize(475, 950);
  169.   glutCreateWindow("GLUT geometric shapes");
  170.   glutDisplayFunc(display);
  171.   glutReshapeFunc(reshape);
  172.  
  173.   glClearColor(1.0, 1.0, 1.0, 1.0);
  174.   glColor3f(0.0, 0.0, 0.0);
  175.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  176.   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  177.   glEnable(GL_LIGHT0);
  178.   glEnable(GL_DEPTH_TEST);
  179.   glMatrixMode(GL_PROJECTION);
  180.   gluPerspective( /* field of view in degree */ 22.0,
  181.   /* aspect ratio */ 1.0,
  182.     /* Z near */ 1.0, /* Z far */ 10.0);
  183.   glMatrixMode(GL_MODELVIEW);
  184.   gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
  185.     0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  186.     0.0, 1.0, 0.);      /* up is in postivie Y direction */
  187.   glTranslatef(0.0, 0.0, -3.0);
  188.   glRotatef(25, 1.0, 0.0, 0.0);
  189.   glRotatef(5, 0.0, 1.0, 0.0);
  190.  
  191.   glutMainLoop();
  192.   return 0;             /* ANSI C requires main to return int. */
  193. }
  194.